package com.dao;

import com.DBConfig.DBUtil;
import com.pojo.Car;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class CarDao {
    private static Connection conn;
    static {
        conn= DBUtil.getConn();
    }
    public static List<Car> getAllCarsBySql(String sql){
        Statement stmt=null;
        ResultSet result=null;
        List<Car> cars=new ArrayList<>();
        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sql);
            while (result.next()) {
                Car car = new Car(
                        result.getString("id"),
                        result.getString("name"),
                        result.getString("remark"),
                        result.getString("brand"),
                        result.getString("type"),
                        result.getBigDecimal("price"),
                        result.getBoolean("rent"),
                        result.getBoolean("status"),
                        result.getString("cardNumber"),
                        result.getInt("carPrice"),
                        result.getString("color"));
                cars.add(car);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (result!=null)result.close();
                if (stmt!=null)stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cars;
    }


    public static Car getCarById(String id) {
        String sql="select * from car where  id=?";
        PreparedStatement preps=null;
        ResultSet result=null;
        Car car=null;
        try {
            preps = conn.prepareStatement(sql);
            preps.setString(1,id);
            result=preps.executeQuery();
            while (result.next()) {
                car = new Car(
                        result.getString("id"),
                        result.getString("name"),
                        result.getString("remark"),
                        result.getString("brand"),
                        result.getString("type"),
                        result.getBigDecimal("price"),
                        result.getBoolean("rent"),
                        result.getBoolean("status"),
                        result.getString("cardNumber"),
                        result.getInt("carPrice"),
                        result.getString("color"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preps!=null)preps.close();
                if (result!=null)result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return car;
    }

    public static boolean addCar(Object cars[]) {
        String sql="insert into car values(?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement preps = null;
        try {
            preps = conn.prepareStatement(sql);
            for (int i = 0; i < cars.length; i++) {
                preps.setObject(i+1,cars[i]);
            }
            preps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(preps!=null)preps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    public static void updateRentById(String id, boolean b){
        String sql="update car set rent=? where id=?";
        PreparedStatement ptst = null;
        try {
            ptst = conn.prepareStatement(sql);
            ptst.setBoolean(1,b);
            ptst.setString(2,id);
            ptst.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ptst!=null)ptst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
    public static boolean updateCarPriceById(String id, BigDecimal value) {
        String sql="update car set price=? where id=?";
        PreparedStatement preps = null;
        try {
            preps = conn.prepareStatement(sql);
            preps.setBigDecimal(1,value);
            preps.setString(2,id);
            preps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preps!=null)preps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean updateCarStatusById(String id, boolean value) {
        String sql="update car set status=?,rent=? where id=?";
        PreparedStatement preps = null;
        try {
            preps = conn.prepareStatement(sql);
            preps.setBoolean(1,value);
            preps.setBoolean(2,value);
            preps.setString(3,id);
            preps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preps!=null)preps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }





}
